package com.zhiyou.aifeng.smart_machine

import android.os.Bundle
import android.view.View
import com.zhiyou.aifeng.common.base.gone
import com.zhiyou.aifeng.common.base.visible
import com.zhiyou.aifeng.common.helper.formatDecimal
import com.zhiyou.aifeng.common.helper.get
import com.zhiyou.aifeng.common.helper.getString
import com.zhiyou.aifeng.common.helper.getTimeString
import com.zhiyou.aifeng.common_ui.base.BaseActivity
import com.zhiyou.aifeng.common_ui.base.addItems
import com.zhiyou.aifeng.common_ui.base.loadLinear
import com.zhiyou.aifeng.common_ui.base.refresh
import com.zhiyou.aifeng.common_ui.model.BaseUrl
import com.zhiyou.aifeng.common_ui.model.CommonData
import com.zhiyou.aifeng.utils.setEmptyIcon
import com.zhiyou.aifeng.viewmodel.WalletViewModel
import kotlinx.android.synthetic.main.layout_empty.*
import kotlinx.android.synthetic.main.layout_list.*
import net.idik.lib.slimadapter.SlimAdapter
import org.jetbrains.anko.backgroundColorResource
import org.jetbrains.anko.frameLayout
import org.jetbrains.anko.include

class WalletCashListActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        frameLayout {
            backgroundColorResource = R.color.white
            include<View>(R.layout.layout_list)
        }
        initTitle(title = "提现记录", divider = false)

        swipe_refresh.isRefreshing = true
        getData()
    }

    override fun getViewModel() = get<WalletViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        initList()
    }

    private fun initList() {
        empty_img.setEmptyIcon()
        empty_hint.text = "暂无相关提现信息！"
        swipe_refresh.refresh { getData(1) }
        recycle_list.apply {
            loadLinear(baseContext, swipe_refresh)

            mAdapter = SlimAdapter.create()
                .register<CommonData>(R.layout.item_cash_list) { data, injector ->
                    injector.text(R.id.item_cash_status, data.name)
                        .text(
                            R.id.item_cash_amount,
                            when (data.status) {
                                "2" -> data.amount.formatDecimal()
                                else -> "—${data.amount.formatDecimal()}"
                            }
                        )
                        .text(R.id.item_cash_time, data.create_date.getTimeString())
                }
                .attachTo(this)
        }
    }

    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel().requestModel(
            requestUrl = BaseUrl.walletList,
            params = *arrayOf("userId" to getString("userId")),
            onSuccess = {
                mList.apply {
                    clear()
                    addItems(it)
                }

                mAdapter.updateData(mList)
            },
            onFinally = {
                swipe_refresh.isRefreshing = false
                empty_view.apply { if (mList.isEmpty()) visible() else gone() }
            }
        )
    }

}